package com.example.communitypro.service.impl;

import com.example.communitypro.mapper.CommunityManagementMapper;
import com.example.communitypro.pojo.CommunityInformation;
import com.example.communitypro.pojo.CommunityManagement;
import com.example.communitypro.pojo.UserInformation;
import com.example.communitypro.service.CommunityManagementService;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommunityManagementServiceImpl implements CommunityManagementService {
    @Autowired(required = false)
    private CommunityManagementMapper communityManagementMapper;

    @Override
    public Result addCommunityManagement(CommunityManagement record){
        try {
            if(communityManagementMapper.insertSelective(record)>0)
            {
                return Result.ok().message("新增社区管理信息成功");
            }
            else{
                return Result.error().message("新增社区管理信息失败");
            }
        }catch (Exception e){
            return Result.error().message("新增社区管理信息失败");
        }
    }


    @Override
    public Result dropCommunityManagementByPrimaryKey(Integer managementNo){
        try {
            if(communityManagementMapper.deleteByPrimaryKey(managementNo)>0)
            {
                return Result.ok().message("删除社区管理信息成功");
            }
            else{
                return Result.error().message("删除社区管理信息失败");
            }
        }catch (Exception e){
            return Result.error().message("删除社区管理信息失败");
        }
    }

    @Override
    public Result dropCommunityManagementByUserAndCommunity(Integer userNO,Integer communityNo){
        try {
            if(communityManagementMapper.deleteByUserAndCommunity(userNO,communityNo)>0)
            {
                return Result.ok().message("删除社区管理信息成功");
            }
            else{
                return Result.error().message("删除社区管理信息失败");
            }
        }catch (Exception e){
            return Result.error().message("删除社区管理信息失败");
        }
    }

    @Override
    public Result queryByUser(Integer userNo){
        List<CommunityInformation> communityInformations =communityManagementMapper.selectByUser(userNo);
        try {
            if(communityInformations != null)
            {
                return Result.ok().message("查找社区管理信息成功").data("communityInformations",communityInformations);
            }
            else{
                return Result.error().message("查找社区管理信息失败");
            }
        }catch (Exception e){
            return Result.error().message("查找社区管理信息失败");
        }
    }

    @Override
    public Result queryByCommunity(Integer communityNo){
        List<UserInformation> userInformations =communityManagementMapper.selectByCommunity(communityNo);
        try {
            if(userInformations != null)
            {
                return Result.ok().message("查找社区管理信息成功").data("userInformations",userInformations);
            }
            else{
                return Result.error().message("查找社区管理信息失败");
            }
        }catch (Exception e){
            return Result.error().message("查找社区管理信息失败");
        }
    }
}
